package com.bbs.service;

import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbs.common.dto.ForumListDTO;
import com.bbs.common.entity.Forum;
import com.bbs.common.entity.ForumCollect;
import com.bbs.common.entity.User;
import com.bbs.common.exception.CommonException;
import com.bbs.common.mapper.ForumCollectMapper;
import com.bbs.common.mapper.ForumMapper;
import com.bbs.common.mapper.UserMapper;
import com.bbs.common.utils.RequestContextUtils;
import com.bbs.common.vo.ForumListVo;
import com.bbs.common.vo.PageVo;
import com.bbs.dto.ForumSaveDTO;
import com.bbs.vo.ForumDetailVo;
import com.github.houbb.sensitive.word.core.SensitiveWordHelper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ForumService
 *
 * @author yangjiajia
 * @createdAt 2024/4/5 16:35
 */
@Service
@Log4j2
public class ForumService {

    @Autowired
    private ForumMapper forumMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ForumCollectMapper forumCollectMapper;

    public void save(ForumSaveDTO saveDTO) {
        int userId = RequestContextUtils.getUserId();
        log.info("帖子发布,param:{}", JSONObject.toJSONString(saveDTO));
        Forum forum = new Forum();
        BeanUtils.copyProperties(saveDTO, forum);
        forum.setContent(SensitiveWordHelper.replace(saveDTO.getContent()));
        forum.setContentOrigin(saveDTO.getContent());
        forum.setUserId(userId);
        forum.setDeleteFlag(0);
        forum.setCreateTime(LocalDateTime.now());
        forum.setUpdateTime(LocalDateTime.now());
        forumMapper.insert(forum);
    }

    public List<List<ForumListVo>> list(ForumListDTO listDTO) {
        List<ForumListVo> list = forumMapper.list(listDTO);
        return ListUtil.partition(list, 4);
    }

    /**
     * 我的发布
     **/
    public PageVo<Forum> myList(ForumListDTO listDTO) {
        Page<Forum> queryPage = new Page<>(listDTO.getPage(), listDTO.getPageSize());
        QueryWrapper<Forum> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Forum::getUserId, RequestContextUtils.getUserId()).eq(Forum::getDeleteFlag, 0);
        if (StringUtils.hasText(listDTO.getTitle())) {
            queryWrapper.lambda().like(Forum::getTitle, listDTO.getTitle());
        }
        queryWrapper.lambda().orderByDesc(Forum::getCreateTime);
        IPage<Forum> page = forumMapper.selectPage(queryPage, queryWrapper);
        return new PageVo<>(page);
    }

    public void delete(int id) {
        Forum forum = forumMapper.selectById(id);
        if (null == forum) {
            throw new CommonException("信息未找到");
        }
        forum.setDeleteFlag(1);
        forum.setUpdateTime(LocalDateTime.now());
        forumMapper.updateById(forum);
    }

    /**
     * 帖子详情
     **/
    public ForumDetailVo detail(int id) {
        Forum forum = forumMapper.selectById(id);
        if (forum.getDeleteFlag() == 1) {
            throw new CommonException("帖子已被删除");
        }
        ForumDetailVo vo = new ForumDetailVo();
        BeanUtils.copyProperties(forum, vo);
        User user = userMapper.selectById(forum.getUserId());
        vo.setUserName(user.getName());

        int userId = RequestContextUtils.getUserId();
        if (userId != forum.getUserId()) {
            forum.setViews(forum.getViews() + 1);
            forumMapper.updateById(forum);
        }

        //是否收藏
        QueryWrapper<ForumCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ForumCollect::getForumId, id).eq(ForumCollect::getUserId, userId);
        if (forumCollectMapper.exists(queryWrapper)) {
            vo.setCollectFlag(true);
        }

        return vo;
    }
}
